package com.gopay.common.cps.service.cbrmb.impl;

import com.gopay.common.constants.cbrmb.AllocationOptType;
import com.gopay.common.constants.proccode.ProcCodeEnum;
import com.gopay.common.cps.dao.cbrmb.CbRmbAllocationOrderLogsDAO;
import com.gopay.common.cps.service.cbrmb.CbRmbAllocationOrderLogsService;
import com.gopay.common.domain.cbrmb.CbRmbAllocationOrderLogs;
import com.gopay.common.exception.GopayException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service("cbRmbAllocationOrderLogsService")
public class CbRmbAllocationOrderLogsServiceImpl implements CbRmbAllocationOrderLogsService {
    private static final Logger LOG = LoggerFactory.getLogger(CbRmbAllocationOrderLogsServiceImpl.class);
    @Autowired
    @Qualifier("cbRmbAllocationOrderLogsDAO")
    private CbRmbAllocationOrderLogsDAO cbRmbAllocationOrderLogsDAO;
    
    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor={GopayException.class})
    public Long save(CbRmbAllocationOrderLogs log) throws GopayException {
        if (log == null) {
            throw new GopayException(ProcCodeEnum.PROC_CODE_000F1002, "日志对象不能为空");
        }
        if (log.getOptId() == null) {
            throw new GopayException(ProcCodeEnum.PROC_CODE_000F1002, "操作员ID不能为空");
        }
        if (log.getCreateTime() == null) {
            log.setCreateTime(new Date());
        }
        CbRmbAllocationOrderLogs saved = cbRmbAllocationOrderLogsDAO.save(log);
        if (saved != null) {
            return saved.getId();
        }
        return null;
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor={GopayException.class})
    public void update(CbRmbAllocationOrderLogs log) throws GopayException {
        if (log == null) {
            throw new GopayException(ProcCodeEnum.PROC_CODE_000F1002, "日志对象不能为空");
        }
        if (log.getId() != null) {
            cbRmbAllocationOrderLogsDAO.update(log);
        }
    }

    @Override
    public List<CbRmbAllocationOrderLogs> findByGopayOrderIdAndCreateTime(String gopayOrderId, String startTime,
            String endTime) throws GopayException {
        if (StringUtils.isBlank(gopayOrderId)) {
            throw new GopayException(ProcCodeEnum.PROC_CODE_000F1002, "查询所需的主订单ID不能为空");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date st = null;
        Date et = null;
        try {
            if (StringUtils.isNotBlank(startTime)) {
                st = sdf.parse(startTime);
            } else {
                // 不指定开始时间则从当天0点开始
                Calendar c = Calendar.getInstance();
                StringBuilder builder = new StringBuilder();
                builder.append(c.get(Calendar.YEAR)).append((c.get(Calendar.MONTH) + 1)).append(c.get(Calendar.DAY_OF_MONTH)).append("000000");
                st = sdf.parse(builder.toString());
            }
            if (StringUtils.isNotBlank(endTime)) {
                et = sdf.parse(endTime);
            } else {
                // 不指定结束时间则在当天23:59:59结束
                Calendar c = Calendar.getInstance();
                StringBuilder builder = new StringBuilder();
                builder.append(c.get(Calendar.YEAR)).append((c.get(Calendar.MONTH) + 1)).append(c.get(Calendar.DAY_OF_MONTH)).append("235959");
                et = sdf.parse(builder.toString());
            }
        } catch (ParseException e) {
            throw new GopayException(ProcCodeEnum.PROC_CODE_000F1002, e);
        }
        return cbRmbAllocationOrderLogsDAO.findByGopayOrderIdAndCreateTime(gopayOrderId, st, et);
    }

    @Override
    public List<CbRmbAllocationOrderLogs> findByGopayOrderIdAndOptTypeWithCreateTime(String gopayOrderId,
            AllocationOptType optType, String startTime, String endTime) throws GopayException {
        if (optType == null) {
            return findByGopayOrderIdAndCreateTime(gopayOrderId, startTime, endTime);
        }
        if (StringUtils.isBlank(gopayOrderId)) {
            throw new GopayException(ProcCodeEnum.PROC_CODE_000F1002, "查询所需的主订单ID不能为空");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date st = null;
        Date et = null;
        try {
            if (StringUtils.isNotBlank(startTime)) {
                st = sdf.parse(startTime);
            } else {
                // 不指定开始时间则从当天0点开始
                Calendar c = Calendar.getInstance();
                c.set(Calendar.HOUR_OF_DAY, 0);
                c.set(Calendar.MINUTE, 0);
                c.set(Calendar.SECOND, 0);
                st = c.getTime();
            }
            if (StringUtils.isNotBlank(endTime)) {
                et = sdf.parse(endTime);
            } else {
                // 不指定结束时间则在当天23:59:59结束
                Calendar c = Calendar.getInstance();
                c.set(Calendar.HOUR_OF_DAY, 23);
                c.set(Calendar.MINUTE, 59);
                c.set(Calendar.SECOND, 59);
                et = c.getTime();
            }
        } catch (ParseException e) {
            LOG.error(e.getMessage(), new IllegalStateException(e));
            throw new GopayException(ProcCodeEnum.PROC_CODE_000F1002, e.getMessage());
        }
        return cbRmbAllocationOrderLogsDAO.findByGopayOrderIdAndOptTypeWithCreateTime(gopayOrderId, optType.getValue(), st, et);
    }

}
